package com.dao;

import com.pojo.User;
import com.utils.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @CreateTime: 2022-12-24  15:08
 * @Description: TODO
 * @Version: 1.0.0
 */

public class IUserDAOImpl implements IUserDAO {

    private static Connection connection;

    static {
        try {
            // 拿到连接
            connection = JdbcUtil.getConnection();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }


    @Override
    public int add(User user) {
        try {
            PreparedStatement preparedStatement = connection.prepareStatement("insert into tb_user values(?,?,?)");
            preparedStatement.setInt(1, user.getId());
            preparedStatement.setString(2, user.getUsername());
            preparedStatement.setString(3, user.getPassword());
            boolean execute = preparedStatement.execute();
            if (execute) {
                return 1;
            }
            return 0;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int update(User user) {
        try {
            PreparedStatement preparedStatement = connection.prepareStatement("update tb_user set username = ?,password = ? where id = ?");
            preparedStatement.setString(1, user.getUsername());
            preparedStatement.setString(2, user.getPassword());
            preparedStatement.setInt(3, user.getId());
            boolean execute = preparedStatement.execute();
            if (execute) {
                return 1;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return 0;
    }

    @Override
    public int delete(int id) {
        try {
            PreparedStatement preparedStatement = connection.prepareStatement("delete from tb_user where id = ?");
            preparedStatement.setInt(1, id);
            boolean execute = preparedStatement.execute();
            if (execute) {
                return 1;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return 0;
    }

    @Override
    public User findByUserName(String userName) {
        try {
            PreparedStatement preparedStatement = connection.prepareStatement("select * from tb_user where username = ?");
            preparedStatement.setString(1, userName);
            ResultSet resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                User user = new User();
                user.setId(resultSet.getInt("id"));
                user.setUsername(resultSet.getString("username"));
                user.setPassword(resultSet.getString("password"));
                return user;
            }
            return null;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public List<User> findAllList() {
        try {
            connection.prepareStatement("select * from tb_user");
            ResultSet resultSet = connection.prepareStatement("select * from tb_user").executeQuery();
            List<User> users = new ArrayList<>();
            while (resultSet.next()) {
                User user = new User();
                user.setId(resultSet.getInt("id"));
                user.setUsername(resultSet.getString("username"));
                user.setPassword(resultSet.getString("password"));
                users.add(user);
            }
            return users;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        IUserDAOImpl userDAO = new IUserDAOImpl();
        System.out.println(userDAO.findAllList());
    }

}
